package com.example.user.controller;

import com.example.common.result.R;
import com.example.user.entity.User;
import com.example.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public R<User> getUser(@PathVariable Long id) {
        return R.ok(userService.getById(id));
    }

    @GetMapping("/username/{username}")
    public R<User> getUserByUsername(@PathVariable String username) {
        return R.ok(userService.getUserByUsername(username));
    }

    @PostMapping
    public R<Boolean> save(@RequestBody User user) {
        return R.ok(userService.save(user));
    }

    @PutMapping
    public R<Boolean> update(@RequestBody User user) {
        return R.ok(userService.updateById(user));
    }

    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        return R.ok(userService.removeById(id));
    }

    @PostMapping("/order")
    public R<Boolean> order() throws Exception {
        return R.ok(userService.order());
    }
}
